package com.zuoshen_algorithm;

import java.util.stream.Stream;

/**
 * 选择排序(由小到大)
 * 时间复杂度 O(n2)
 * 比方一个数组【4，2，3，1】，将其按照从小到达排列
 * 其实就是先从第一个开始遍历，找到一个最小值放到首位，然后从第二个开始遍历，找到第二小的数放到第二位，以此类推
 */
public class XuanZeSort {

    public static void main(String[] args) {
        Integer[] num = {4, 1, 2, 7, 8, 5, 3};
        Stream.of(num).forEach(x -> System.out.print(x));
        System.out.println();
        sort1(num);
        Stream.of(num).forEach(x -> System.out.print(x));
    }

    public static void sort1(Integer[] num) {
        for (int i = 0; i < num.length; i++) {
            int minNum = i;
            for (int j = i+1; j < num.length; j++) {
                //获取到每次的最小值下标
                minNum = num[j] < num[minNum] ? j : minNum;
            }
            if (i!=minNum){
                Util.swap(num, i, minNum);
            }
        }
    }

    public static void sort(Integer[] num) {
        int temp = Integer.MAX_VALUE;
        for (int i = 0; i < num.length; i++) {
            for (int j = i; j < num.length; j++) {
                if (num[j] < temp) {
                    num[j] = num[j] ^ temp;
                    temp = num[j] ^ temp;
                    num[j] = num[j] ^ temp;
                }
            }
            num[i] = temp;
            temp = Integer.MAX_VALUE;
        }
    }


}
